package de.gwtprime.client.activities;

import java.util.List;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.Request;
import com.google.web.bindery.requestfactory.shared.ServerFailure;

import de.gwtprime.client.ClientFactory;
import de.gwtprime.client.PrimeRequestFactory;
import de.gwtprime.client.event.VoteQuestionLoadedEvent;
import de.gwtprime.client.event.VotesAnswerCompleteLoadedEvent;
import de.gwtprime.client.event.VotesAnswerLoadedEvent;
import de.gwtprime.client.places.QuestionPlace;
import de.gwtprime.client.util.AppUtils;
import de.gwtprime.client.util.ErrorWidget;
import de.gwtprime.client.util.UserInputParser;
import de.gwtprime.client.views.QuestionView;
import de.gwtprime.client.viewsStatic.LoadingIndictator;
import de.gwtprime.client.viewsStatic.NavigationViewImpl.Section;
import de.gwtprime.shared.requestFactory.QuestionProxy;
import de.gwtprime.shared.requestFactory.VoteAnswerCompletedProxy;
import de.gwtprime.shared.requestFactory.VoteAnswerProxy;
import de.gwtprime.shared.requestFactory.VoteQuestionProxy;
import de.gwtprime.shared.requestFactory.request.QuestionRequest;

public class QuestionActivity extends PrimeAbstractActivity implements
		QuestionView.Presenter {

	private ClientFactory clientFactory;

	private String name;

	/**
	 * Logger
	 */
	// private static Logger logger =
	// Logger.getLogger(ArticleActivity.class.getName());

	public QuestionActivity(QuestionPlace place, ClientFactory clientFactory) {
		this.name = place.getName();
		this.clientFactory = clientFactory;
	}

	QuestionView questionView;

	@Override
	public void start(final AcceptsOneWidget panel, final EventBus eventBus) {
		
		clientFactory.getNavigationView().activateSection(Section.ARTICLE);
		
		panel.setWidget(new LoadingIndictator());
		
		clientFactory.getGreenHeadView().setVisible(false);
		questionView = clientFactory.getQuestionView();
		
		

		final PrimeRequestFactory requestFactory = clientFactory
				.getRequestFactory();

		final QuestionRequest request = requestFactory.questionRequest();
		Request<QuestionProxy> createReq = request.findQuestion(
				Long.parseLong(name)).with("user", "answers", "answers.user");
		createReq.fire(new Receiver<QuestionProxy>() {
			@Override
			public void onSuccess(QuestionProxy question) {
				
				if(question==null){
					panel.setWidget(new ErrorWidget("Question not found"));
					return;
				}
				
				panel.setWidget(questionView.asWidget());
				questionView.setQuestion(question);
				UserInputParser.prettyPrint();
				
				loadQuestionVote(requestFactory, question);
				
				loadAnswerVotes(requestFactory, question);
				
				AppUtils.trackPageview("Question:"+ question.getId());
				
			}

			private void loadQuestionVote(final PrimeRequestFactory requestFactory, QuestionProxy question) {
				if(clientFactory.getUser()!=null){
					final QuestionRequest votesCompReq = requestFactory.questionRequest();
					Request<VoteQuestionProxy> createVotesCompReq = votesCompReq.getVoteQuestionForUser(clientFactory.getUser()).using(question);
					createVotesCompReq.fire(new Receiver<VoteQuestionProxy>() {
						@Override
						public void onSuccess(VoteQuestionProxy vote) {

							if(vote!=null){
								clientFactory.getEventBus().fireEvent(new VoteQuestionLoadedEvent(vote));
							}else{
								AppUtils.log("No vote found for question");
							}
						}

						@Override
						public void onFailure(ServerFailure error) {
							AppUtils.log("Failed to load vote");
						}
					});
				}
			}

			private void loadAnswerVotes(
					final PrimeRequestFactory requestFactory, QuestionProxy question) {
				if(clientFactory.getUser()!=null){
					
					
					
					
					final QuestionRequest votesCompReq = requestFactory.questionRequest();
					Request<List<VoteAnswerCompletedProxy>> createVotesCompReq = votesCompReq.getAllAnswerVotesCompleteForUser(clientFactory.getUser()).using(question);
					createVotesCompReq.fire(new Receiver<List<VoteAnswerCompletedProxy>>() {
						@Override
						public void onSuccess(List<VoteAnswerCompletedProxy> votes) {
							clientFactory.getEventBus().fireEvent(new VotesAnswerCompleteLoadedEvent(votes));
						}

						@Override
						public void onFailure(ServerFailure error) {
							System.out.println("failed to load votes");
						}
					});
					
					
					final QuestionRequest request2 = requestFactory.questionRequest();
					Request<List<VoteAnswerProxy>> createReq2 = request2.getAllAnswerVotesForUser(clientFactory.getUser()).using(question);
					createReq2.fire(new Receiver<List<VoteAnswerProxy>>() {
						@Override
						public void onSuccess(List<VoteAnswerProxy> votes) {
							clientFactory.getEventBus().fireEvent(new VotesAnswerLoadedEvent(votes));
						}

						@Override
						public void onFailure(ServerFailure error) {
							System.out.println("failed to load votes");
						}
					});
				}
			}

			@Override
			public void onFailure(ServerFailure error) {
				panel.setWidget(new ErrorWidget("An error occured while loading the question"));
				System.out.println("error");
			}
		});
		
	}

	@Override
	public void goTo(Place place) {
		clientFactory.getPlaceController().goTo(place);
	}

}
